<?php

namespace Admin\Controller;
use Think\Controller;

class BaseController extends Controller{
	
	public function _initialize(){
		
		if(CONTROLLER_NAME!='Login'){
			if(empty($_SESSION['admin'])){
				$this->redirect('Login/index');
                exit;
			}
		}
        
        //权限处理
        $admin = $_SESSION['admin'];
        if ($admin['role_id'] != 0) {
            $access = D('access')->where(array('role_id'=>$admin['role_id']))->find();
            $accessid = explode("#",$access['nav_id']);
            $navaction = strtolower(CONTROLLER_NAME . '/' . ACTION_NAME);
            $i = 1;
            foreach($accessid as $key => $value){
                $navlist[$i] = D('nav')->find($value);$i++;
            }
            $navid = 0;
            foreach ($navlist as $k => $v) {
                if (strtolower($v['controller'].'/'.$v['function']) == $navaction) {
                    $navid = (int)$k;
                    break;
                }
            }
            
            if (empty($navid)) {
                $controller_name = strtolower(CONTROLLER_NAME);
                $action_name = strtolower(ACTION_NAME);
                $nav_name = D('nav') ->where("controller = '{$controller_name}' and function = '{$action_name}'")->getField('nav_name');
                $this->error('很抱歉您没有权限操作模块:' . $nav_name, U('Admin/index/index'));
            }
			$admininfo = M('agent')->where("id = {$admin['id']}")->find();
			session('admin',$admininfo);
        }else{
			
            $navlist = D('nav')->where(['namespace'=>"Admin"])->select();
        }
        
        $this->assign(array(
            'navlist'=>$navlist
        ));
		
	}
	
	
}


?>